package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.WxAddressListVo;
import com.cskaoyan.bean.vo.WxAddressVo;
import com.cskaoyan.service.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 微信收货地址功能
 * @author Fyy
 * @since 2023/01/01 15:00
 */

@RestController
@RequestMapping("wx/address")
public class WxAddressController {

    @Autowired
    WxAddressService addressService;

    /**
     * 收货地址列表
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(){
        //获取用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        //获取用户id
        Integer id = marketUser.getId();
        //根据用户id查询收货地址表
        List<MarketAddress> list = addressService.list(id);
        //封装返回值需要的收货地址表
        List<WxAddressVo> listVos = new ArrayList<>();
        for (MarketAddress marketAddress : list) {
            //判空并赋值
            if (!ObjectUtils.isEmpty(marketAddress)){
                WxAddressVo wxAddressListVo = new WxAddressVo();
                BeanUtils.copyProperties(marketAddress,wxAddressListVo);
                listVos.add(wxAddressListVo);
            }
        }
        //封装返回值
        WxAddressListVo wxAddressListVo = new WxAddressListVo();
        wxAddressListVo.setList(listVos);
        wxAddressListVo.setLimit(listVos.size());
        wxAddressListVo.setTotal(listVos.size());
        return BaseRespVo.ok(wxAddressListVo,"成功");
    }

    /**
     * 新增收货地址
     * @param marketAddress
     * @return
     */
    @RequestMapping("save")
    public BaseRespVo save(@RequestBody MarketAddress marketAddress){
        //更新添加时间
        marketAddress.setAddTime(new Date());
        //更新修改时间
        marketAddress.setUpdateTime(new Date());
        //获取用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        Integer userId = marketUser.getId();
        //给地址信息赋值
        marketAddress.setUserId(userId);
        Integer id = addressService.save(marketAddress);
        return BaseRespVo.ok(id,"成功");
    }
    // @RequestMapping("save")
    // public BaseRespVo save(@RequestBody MarketAddress marketAddress){
    //     //更新添加时间
    //     marketAddress.setAddTime(new Date());
    //     //更新修改时间
    //     marketAddress.setUpdateTime(new Date());
    //     //获取用户信息
    //     Subject subject = SecurityUtils.getSubject();
    //     MarketUser marketUser = (MarketUser) subject.getPrincipal();
    //     Integer userId = marketUser.getId();
    //     //给地址信息赋值
    //     marketAddress.setUserId(userId);
    //     Integer id = addressService.save(marketAddress);
    //     return BaseRespVo.ok(id,"成功");
    // }

    /**
     * 收货地址详情
     * @param id
     * @return
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){
        //根据id查询收货地址详情
        MarketAddress marketAddress = addressService.detail(id);
        //封装返回值
        WxAddressVo wxAddressVo = new WxAddressVo();
        BeanUtils.copyProperties(marketAddress,wxAddressVo);
        return BaseRespVo.ok(wxAddressVo,"成功");
    }

    /**
     * 删除收货地址
     * @param marketAddress
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketAddress marketAddress){
        //逻辑删除
        marketAddress.setDeleted(true);
        addressService.delete(marketAddress);
        return BaseRespVo.ok("成功");
    }
}
